package muhnes.sizoo.vista;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import muhnes.sizoo.dal.FabricaDAO;
import muhnes.sizoo.dal.FamiliaDao;
import muhnes.sizoo.dal.FamiliaDaoImpl;
import muhnes.sizoo.modelo.Familia;

public class FamiliaConverter implements Converter {
    
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        Object valorObject = null;
        
        if( value != null && value != "" ) {
            FamiliaDao familiaDao = FabricaDAO.crearFamiliaDao();
            valorObject = familiaDao.removerProxy(
                    familiaDao.buscarPorId( Integer.parseInt( value ) ));
        }
        
        return valorObject;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        String valorString = "";
        
        if( value != null && value instanceof Familia ) {
            valorString = String.valueOf( ((Familia) value).getCodigofamilia() );
        }
        
        return valorString;
    }

}
